package com.wm.user.controller;

import com.wm.user.entity.Student;
import com.wm.user.service.StudentService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;


/**
 * Created by asus on 2017/7/4.
 */

@Controller
@RequestMapping(value = "user")
public class UserController {




    @Autowired
    private StudentService studentService;

//    @RequestMapping(value = "test/{name}/{age}")
    @RequestMapping(value = "test")
//    @ResponseBody
    public String testUser(@RequestBody Student student){
//    	User user = new User();	
//    	user.setName(name);
//    	user.setAge(age);
//    	map.addAttribute(user);
//        return "redirect:/user.jsp?name="+name+"&age="+age;
        studentService.createStudent(student);

        return "user";
    }
    
    @RequestMapping(value = "user")
    public String toUser(){
    	
    	return "user";
    }
    
    @RequestMapping(value = "upload")
    public String upload(HttpServletRequest request) throws Exception { 
    	// 创建一个通用的多部分解析器
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
				request.getSession().getServletContext());
		//判断 request 是否有文件上传,即多部分请求  
        if(multipartResolver.isMultipart(request)){  
            //转换成多部分request    
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;  
            //取得request中的所有文件名  
            Iterator<String> iter = multiRequest.getFileNames();  
            while(iter.hasNext()){
                //取得上传文件  
                MultipartFile file = multiRequest.getFile(iter.next());  
                String fileName = file.getOriginalFilename();
                file.transferTo(new File("/BaofengDownload/" + fileName));	    
            }    
        }
    
    	return "user";
    }
    
    @RequestMapping(value = "download")
//    public String download(HttpServletRequest request, HttpServletResponse response) throws Exception {
//    	String fileName = "D:\\我是.png";
//    	File file = new File(fileName);
//    	response.setCharacterEncoding("utf-8");
//		response.setContentType("multipart/form-data");
//		response.setHeader("Content-Disposition", "attachment;fileName=" 
//				+ new String(file.getName().getBytes("UTF-8"),"ISO8859-1"));
//		InputStream inputStream = new FileInputStream(file);
//		OutputStream os = response.getOutputStream();
//		byte[] b = new byte[2048];
//		int length;
//		while ((length = inputStream.read(b)) > 0) {
//			os.write(b, 0, length);
//		}
//		os.close();
//		inputStream.close();
//		return null;
//	}
    public ResponseEntity<byte[]> download() throws IOException {
    	
    	String filePath = "D:\\我是.png";
    	File file = new File(filePath);
    	
    	HttpHeaders headers = new HttpHeaders();
    	//文件名中文乱码
    	headers.setContentDispositionFormData("attachment", 
    	new String(filePath.getBytes("UTF-8"),"ISO8859-1")); 
    	//设置文件类型
    	headers.setContentType(MediaType.IMAGE_PNG);
    	
    	return new ResponseEntity<byte[]> (
    			FileUtils.readFileToByteArray(file),
    			headers, HttpStatus.CREATED); 
    }
    
}
